<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <title>egg-schedule - 为企业级框架和应用而生</title>
  <meta charset="utf-8">
  <meta name="description" content="index.description">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <link rel="icon" href="/images/favicon.png" type="image/x-icon">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css" />
<link rel="stylesheet" href="/css/index.css">

    <script>
    !function(t,e,a,r,c){t.TracertCmdCache=t.TracertCmdCache||[],t[c]=window[c]||
      {_isInit:!0,call:function(){t.TracertCmdCache.push(arguments)},
      start:function(t){this.call('start',t)}},t[c].l=new Date;
      var n=e.createElement(a),s=e.getElementsByTagName(a)[0];
      n.async=!0,n.src=r,s.parentNode.insertBefore(n,s)}
    (window,document,'script','https://tracert.alipay.com/tracert.js','Tracert');
      Tracert.start({
        plugins: [ 'BucName' ],
        spmAPos: 'a454',
        spmBPos: 'b4893',
      });
    </script>
  
</head>
<body>
  <div class="nav" >
  <header>
    <a href="/zh-cn/" class="nav-logo leftpadding" alt="egg"><img src="https://zos.alipayobjects.com/rmsportal/VTcUYAaoKqXyHJbLAPyF.svg"></a>
    <ul class="nav-item">
      <li>
        <form id="search-form">
          <input type="text" id="search-query" class="search-query st-default-search-input">
        </form>
      </li>
      <li><a href="/zh-cn/intro/" alt="指南">指南</a></li><li><a href="/api/" alt="API">API</a></li><li><a href="/zh-cn/tutorials/index.html" alt="教程">教程</a></li><li><a href="https://github.com/search?q=topic%3Aegg-plugin&type=Repositories" alt="插件">插件</a></li><li><a href="https://github.com/eggjs/egg/releases" alt="发布日志">发布日志</a></li>
      
      
        <li class="translations">
          <a class="nav-link">Translations</a>
          <span class="arrow"></span><ul id="dropdownContent" class="dropdown-content"><li><a id="en" href="/en/plugins/schedule.html" >English</a></li><li><a id="zh-cn" href="/zh-cn/plugins/schedule.html" style="color: #22ab28">中文</a></li></ul>
        </li>
      
      <li><iframe src="https://ghbtns.com/github-btn.html?user=eggjs&repo=egg&type=star&count=true" frameborder="0" scrolling="0" width="150px" height="20px"></iframe></li>
    </ul>
    <a id="mobileTrigger" href="#" class="mobile-trigger">
      <ul>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </a>
  </header>
</div>
  <div id="container" class="container">
    <div class="page-main">
  <article class="markdown-body">
    <h1>egg-schedule</h1>
    <p><a href="https://npmjs.org/package/egg-schedule" target="_blank" rel="noopener"><img src="https://img.shields.io/npm/v/egg-schedule.svg?style=flat-square" alt="NPM version"></a>
<a href="https://travis-ci.org/eggjs/egg-schedule" target="_blank" rel="noopener"><img src="https://img.shields.io/travis/eggjs/egg-schedule.svg?style=flat-square" alt="build status"></a>
<a href="https://codecov.io/github/eggjs/egg-schedule?branch=master" target="_blank" rel="noopener"><img src="https://codecov.io/github/eggjs/egg-schedule/coverage.svg?branch=master" alt="Test coverage"></a>
<a href="https://david-dm.org/eggjs/egg-schedule" target="_blank" rel="noopener"><img src="https://img.shields.io/david/eggjs/egg-schedule.svg?style=flat-square" alt="David deps"></a>
<a href="https://snyk.io/test/npm/egg-schedule" target="_blank" rel="noopener"><img src="https://snyk.io/test/npm/egg-schedule/badge.svg?style=flat-square" alt="Known Vulnerabilities"></a>
<a href="https://npmjs.org/package/egg-schedule" target="_blank" rel="noopener"><img src="https://img.shields.io/npm/dm/egg-schedule.svg?style=flat-square" alt="npm download"></a></p>
<p>A schedule plugin for egg. It supports two scheduler types, <code>worker</code> and <code>all</code>, and can be extended by other plugins.</p>
<p><code>egg-schedule</code> has been built-in for egg. It is enabled by default.</p>
<h2 id="usage"><a class="markdown-anchor" href="#usage">#</a> Usage</h2>
<p>Just add you job file to <code>{app_root}/app/schedule</code>.</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// &#123;app_root&#125;/app/schedule/cleandb.js</span></span><br><span class="line"><span class="keyword">const</span> Subscription = <span class="built_in">require</span>(<span class="string">'egg'</span>).Subscription;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CleanDB</span> <span class="keyword">extends</span> <span class="title">Subscription</span> </span>&#123;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * @property &#123;Object&#125; schedule</span></span><br><span class="line"><span class="comment">   *  - &#123;String&#125; type - schedule type, `worker` or `all`</span></span><br><span class="line"><span class="comment">   *  - &#123;String&#125; [cron] - cron expression, see [below](#cron-style-scheduling)</span></span><br><span class="line"><span class="comment">   *  - &#123;Object&#125; [cronOptions] - cron options, see [cron-parser#options](https://github.com/harrisiirak/cron-parser#options)</span></span><br><span class="line"><span class="comment">   *  - &#123;String | Number&#125; [interval] - interval expression in millisecond or express explicitly like '1h'. see [below](#interval-style-scheduling)</span></span><br><span class="line"><span class="comment">   *  - &#123;Boolean&#125; [immediate] - To run a scheduler at startup</span></span><br><span class="line"><span class="comment">   *  - &#123;Boolean&#125; [disable] - whether to disable a scheduler, usually use in dynamic schedule</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="keyword">static</span> get schedule() &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      type: <span class="string">'worker'</span>,</span><br><span class="line">      cron: <span class="string">'0 0 3 * * *'</span>,</span><br><span class="line">      <span class="comment">// interval: '1h',</span></span><br><span class="line">      <span class="comment">// immediate: true,</span></span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">async</span> subscribe() &#123;</span><br><span class="line">    <span class="keyword">await</span> <span class="keyword">this</span>.ctx.service.db.cleandb();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = CleanDB;</span><br></pre></td></tr></table></figure>
<p>You can also use function simply.</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">exports.schedule = &#123;</span><br><span class="line">  type: <span class="string">'worker'</span>,</span><br><span class="line">  cron: <span class="string">'0 0 3 * * *'</span>,</span><br><span class="line">  <span class="comment">// interval: '1h',</span></span><br><span class="line">  <span class="comment">// immediate: true,</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">exports.task = <span class="keyword">async</span> <span class="function"><span class="keyword">function</span> (<span class="params">ctx</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">await</span> ctx.service.db.cleandb();</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="overview"><a class="markdown-anchor" href="#overview">#</a> Overview</h2>
<p><code>egg-schedule</code> supports both time-based scheduling and interval-based scheduling.</p>
<p>Schedule decision is being made by <code>agent</code> process. <code>agent</code> triggers a task and sends message to <code>worker</code> process. Then, one or all <code>worker</code> process(es) execute the task based on schedule type.</p>
<p>To setup a schedule task, simply create a job file in <code>{app_root}/app/schedule</code>. A file contains one job and export <code>schedule</code> and <code>task</code> properties.</p>
<p>The rule of thumbs is one job per file.</p>
<h2 id="task"><a class="markdown-anchor" href="#task">#</a> Task</h2>
<p>Task is a class which will be instantiated every schedule, and <code>subscribe</code> method will be invoked.</p>
<p>You can get anonymous context with <code>this.ctx</code>.</p>
<ul>
<li>ctx.method: <code>SCHEDULE</code></li>
<li>ctx.path: <code>/__schedule?path=${schedulePath}&amp;${schedule}</code>.</li>
</ul>
<p>To create a task, <code>subscribe</code> can be generator function or async function. For example:</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// A simple logger example</span></span><br><span class="line"><span class="keyword">const</span> Subscription = <span class="built_in">require</span>(<span class="string">'egg'</span>).Subscription;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LoggerExample</span> <span class="keyword">extends</span> <span class="title">Subscription</span> </span>&#123;</span><br><span class="line">  * subscribe() &#123;</span><br><span class="line">    <span class="keyword">this</span>.ctx.logger.info(<span class="string">'Info about your task'</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// A real world example: wipe out your database.</span></span><br><span class="line"><span class="comment">// Use it with caution. :)</span></span><br><span class="line"><span class="keyword">const</span> Subscription = <span class="built_in">require</span>(<span class="string">'egg'</span>).Subscription;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CleanDB</span> <span class="keyword">extends</span> <span class="title">Subscription</span> </span>&#123;</span><br><span class="line">  <span class="keyword">async</span> subscribe() &#123;</span><br><span class="line">    <span class="keyword">await</span> <span class="keyword">this</span>.ctx.service.db.cleandb();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="scheduling"><a class="markdown-anchor" href="#scheduling">#</a> Scheduling</h2>
<p><code>schedule</code> is an object that contains one required property, <code>type</code>, and optional properties, <code>{ cron, cronOptions, interval, immediate, disable }</code>.</p>
<h3 id="cron-style-scheduling"><a class="markdown-anchor" href="#cron-style-scheduling">#</a> Cron-style Scheduling</h3>
<p>Use <a href="https://github.com/harrisiirak/cron-parser" target="_blank" rel="noopener">cron-parser</a>.</p>
<blockquote>
<p>Note: <code>cron-parser</code> support <code>second</code> as optional that is not supported by linux crontab.</p>
<p><code>@hourly / @daily / @weekly / @monthly / @yearly</code> is also supported.</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">*    *    *    *    *    *</span><br><span class="line">┬    ┬    ┬    ┬    ┬    ┬</span><br><span class="line">│    │    │    │    │    |</span><br><span class="line">│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)</span><br><span class="line">│    │    │    │    └───── month (1 - 12)</span><br><span class="line">│    │    │    └────────── day of month (1 - 31)</span><br><span class="line">│    │    └─────────────── hour (0 - 23)</span><br><span class="line">│    └──────────────────── minute (0 - 59)</span><br><span class="line">└───────────────────────── second (0 - 59, optional)</span><br></pre></td></tr></table></figure>
<p>Example:</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// To execute task every 3 hours</span></span><br><span class="line">exports.schedule = &#123;</span><br><span class="line">  type: <span class="string">'worker'</span>,</span><br><span class="line">  cron: <span class="string">'0 0 */3 * * *'</span>,</span><br><span class="line">  cronOptions: &#123;</span><br><span class="line">    <span class="comment">// tz: 'Europe/Athens',</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="interval-style-scheduling"><a class="markdown-anchor" href="#interval-style-scheduling">#</a> Interval-style Scheduling</h3>
<p>To use <code>setInterval</code>, and support <a href="https://www.npmjs.com/package/ms" target="_blank" rel="noopener">ms</a> conversion style</p>
<p>Example:</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// To execute task every 3 hours</span></span><br><span class="line">exports.schedule = &#123;</span><br><span class="line">  type: <span class="string">'worker'</span>,</span><br><span class="line">  interval: <span class="string">'3h'</span>,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="schedule-type"><a class="markdown-anchor" href="#schedule-type">#</a> Schedule Type</h3>
<p><strong>Build-in support is:</strong></p>
<ul>
<li><strong>worker</strong>: will be executed in one random worker when schedule run.</li>
<li><strong>all</strong>: will be executed in all workers when schedule run.</li>
</ul>
<p><strong>Custom schedule</strong></p>
<p>To create a custom schedule, simply extend <code>agent.ScheduleStrategy</code> and register it by <code>agent.schedule.use(type, clz)</code>.
You can schedule the task to be executed by one random worker or all workers with the built-in method <code>this.sendOne(...args)</code> or <code>this.sendAll(...args)</code> which support params, it will pass to <code>subscribe(...args)</code> or <code>task(ctx, ...args)</code>.</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// &#123;app_root&#125;/agent.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="keyword">function</span>(<span class="params">agent</span>) </span>&#123;</span><br><span class="line">  <span class="class"><span class="keyword">class</span> <span class="title">CustomStrategy</span> <span class="keyword">extends</span> <span class="title">agent</span>.<span class="title">ScheduleStrategy</span> </span>&#123;</span><br><span class="line">    start() &#123;</span><br><span class="line">      <span class="comment">// such as mq / redis subscribe</span></span><br><span class="line">      agent.notify.subscribe(<span class="string">'remote_task'</span>, data =&gt;</span><br><span class="line">        <span class="keyword">this</span>.sendOne(data);</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  agent.schedule.use(<span class="string">'custsom'</span>, CustomStrategy);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>Then you could use it to defined your job:</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// &#123;app_root&#125;/app/schedule/other.js</span></span><br><span class="line"><span class="keyword">const</span> Subscription = <span class="built_in">require</span>(<span class="string">'egg'</span>).Subscription;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ClusterTask</span> <span class="keyword">extends</span> <span class="title">Subscription</span> </span>&#123;</span><br><span class="line">  <span class="keyword">static</span> get schedule() &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      type: <span class="string">'custom'</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">async</span> subscribe(data) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'got custom data:'</span>, data);</span><br><span class="line">    <span class="keyword">await</span> <span class="keyword">this</span>.ctx.service.someTask.run();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="dynamic-schedule"><a class="markdown-anchor" href="#dynamic-schedule">#</a> Dynamic schedule</h2>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// &#123;app_root&#125;/app/schedule/sync.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="class"><span class="keyword">class</span> <span class="title">SyncTask</span> <span class="keyword">extends</span> <span class="title">app</span>.<span class="title">Subscription</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> get schedule() &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">        interval: <span class="number">10000</span>,</span><br><span class="line">        type: <span class="string">'worker'</span>,</span><br><span class="line">        <span class="comment">// only start task when hostname match</span></span><br><span class="line">        disable: <span class="built_in">require</span>(<span class="string">'os'</span>).hostname() !== app.config.sync.hostname</span><br><span class="line">      &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">async</span> subscribe() &#123;</span><br><span class="line">      <span class="keyword">await</span> ctx.sync();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> SyncTask;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="logging"><a class="markdown-anchor" href="#logging">#</a> Logging</h2>
<p>See <code>${appInfo.root}/logs/{app_name}/egg-schedule.log</code> which provided by <a href="https://github.com/eggjs/egg-schedule/blob/master/config/config.default.js" target="_blank" rel="noopener">config.customLogger.scheduleLogger</a>.</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// config/config.default.js</span></span><br><span class="line">config.customLogger = &#123;</span><br><span class="line">  scheduleLogger: &#123;</span><br><span class="line">    <span class="comment">// consoleLevel: 'NONE',</span></span><br><span class="line">    <span class="comment">// file: path.join(appInfo.root, 'logs', appInfo.name, 'egg-schedule.log'),</span></span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="testing"><a class="markdown-anchor" href="#testing">#</a> Testing</h2>
<p><code>app.runSchedule(scheduleName)</code> is provided by <code>egg-schedule</code> plugin only for test purpose.</p>
<p>Example:</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">it(<span class="string">'test a schedule task'</span>, <span class="keyword">async</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// get app instance</span></span><br><span class="line">  <span class="keyword">await</span> app.runSchedule(<span class="string">'clean_cache'</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h2 id="questions-suggestions"><a class="markdown-anchor" href="#questions-suggestions">#</a> Questions &amp; Suggestions</h2>
<p>Please open an issue <a href="https://github.com/eggjs/egg/issues" target="_blank" rel="noopener">here</a>.</p>
<h2 id="license"><a class="markdown-anchor" href="#license">#</a> License</h2>
<p><a href="https://github.com/eggjs/egg-schedule/blob/master/LICENSE" target="_blank" rel="noopener">MIT</a></p>

  </article>
  <aside id="mobileAside" class="toc">
  <div class="mobile-menu">
    <ul>
      <li><a href="/zh-cn/intro/" alt="指南">指南</a></li><li><a href="/api/" alt="API">API</a></li><li><a href="/zh-cn/tutorials/index.html" alt="教程">教程</a></li><li><a href="https://github.com/search?q=topic%3Aegg-plugin&type=Repositories" alt="插件">插件</a></li><li><a href="https://github.com/eggjs/egg/releases" alt="发布日志">发布日志</a></li>
      
      
        <li class="translations">
          <a class="nav-link">Translations</a>
          <span class="arrow"></span><ul id="dropdownContent" class="dropdown-content"><li><a id="en" href="/en/plugins/schedule.html" >English</a></li><li><a id="zh-cn" href="/zh-cn/plugins/schedule.html" style="color: #22ab28">中文</a></li></ul>
        </li>
      
    </ul>
  </div>
  <dl><dt id="title-Intro" style="cursor: pointer;" class="aside-title">新手指南<a id="collapse-icon-Intro" class="icon opend"></a></dt><dd id=panel-Intro><ul><li><a href="/zh-cn/intro/index.html" class="menu-link">Egg.js 是什么?</a></li><li><a href="/zh-cn/intro/egg-and-koa.html" class="menu-link">Egg.js 和 Koa</a></li><li><a href="/zh-cn/intro/quickstart.html" class="menu-link">快速入门</a></li><li><a href="/zh-cn/tutorials/progressive.html" class="menu-link">渐进式开发</a></li><li><a href="/zh-cn/migration.html" class="menu-link">2.x 升级指南</a></li></ul></dd><dt id="title-Basics" style="cursor: pointer;" class="aside-title">基础功能<a id="collapse-icon-Basics" class="icon opend"></a></dt><dd id=panel-Basics><ul><li><a href="/zh-cn/basics/structure.html" class="menu-link">目录结构</a></li><li><a href="/zh-cn/basics/objects.html" class="menu-link">内置对象</a></li><li><a href="/zh-cn/basics/env.html" class="menu-link">运行环境</a></li><li><a href="/zh-cn/basics/config.html" class="menu-link">配置</a></li><li><a href="/zh-cn/basics/middleware.html" class="menu-link">中间件</a></li><li><a href="/zh-cn/basics/router.html" class="menu-link">Router</a></li><li><a href="/zh-cn/basics/controller.html" class="menu-link">Controller</a></li><li><a href="/zh-cn/basics/service.html" class="menu-link">Service</a></li><li><a href="/zh-cn/basics/plugin.html" class="menu-link">插件</a></li><li><a href="/zh-cn/basics/schedule.html" class="menu-link">定时任务</a></li><li><a href="/zh-cn/basics/extend.html" class="menu-link">框架扩展</a></li><li><a href="/zh-cn/basics/app-start.html" class="menu-link">启动自定义</a></li></ul></dd><dt id="title-Core" style="cursor: pointer;" class="aside-title">核心功能<a id="collapse-icon-Core" class="icon opend"></a></dt><dd id=panel-Core><ul><li><a href="/zh-cn/core/development.html" class="menu-link">本地开发</a></li><li><a href="/zh-cn/core/unittest.html" class="menu-link">单元测试</a></li><li><a href="/zh-cn/core/deployment.html" class="menu-link">应用部署</a></li><li><a href="/zh-cn/core/logger.html" class="menu-link">日志</a></li><li><a href="/zh-cn/core/httpclient.html" class="menu-link">HttpClient</a></li><li><a href="/zh-cn/core/cookie-and-session.html" class="menu-link">Cookie and Session</a></li><li><a href="/zh-cn/core/cluster-and-ipc.html" class="menu-link">多进程模型和进程间通讯</a></li><li><a href="/zh-cn/core/view.html" class="menu-link">模板渲染</a></li><li><a href="/zh-cn/core/error-handling.html" class="menu-link">异常处理</a></li><li><a href="/zh-cn/core/security.html" class="menu-link">安全</a></li><li><a href="/zh-cn/core/i18n.html" class="menu-link">国际化</a></li></ul></dd><dt id="title-Tutorials" style="cursor: pointer;" class="aside-title">教程<a id="collapse-icon-Tutorials" class="icon opend"></a></dt><dd id=panel-Tutorials><ul><li><a href="/zh-cn/tutorials/mysql.html" class="menu-link">MySQL</a></li><li><a href="/zh-cn/tutorials/restful.html" class="menu-link">RESTful API</a></li><li><a href="/zh-cn/tutorials/passport.html" class="menu-link">Passport 鉴权</a></li><li><a href="/zh-cn/tutorials/socketio.html" class="menu-link">Socket.IO</a></li><li><a href="/zh-cn/tutorials/assets.html" class="menu-link">静态资源</a></li><li><a href="/zh-cn/tutorials/typescript.html" class="menu-link">TypeScript</a></li></ul></dd><dt id="title-Advanced" style="cursor: pointer;" class="aside-title">进阶<a id="collapse-icon-Advanced" class="icon opend"></a></dt><dd id=panel-Advanced><ul><li><a href="/zh-cn/advanced/loader.html" class="menu-link">Loader</a></li><li><a href="/zh-cn/advanced/plugin.html" class="menu-link">插件开发</a></li><li><a href="/zh-cn/advanced/framework.html" class="menu-link">框架开发</a></li><li><a href="/zh-cn/advanced/cluster-client.html" class="menu-link">多进程研发模式增强</a></li><li><a href="/zh-cn/advanced/view-plugin.html" class="menu-link">模板插件开发规范</a></li><li><a href="/zh-cn/style-guide.html" class="menu-link">代码风格指南</a></li></ul></dd><dt id="title-Community" style="cursor: pointer;" class="aside-title">社区<a id="collapse-icon-Community" class="icon opend"></a></dt><dd id=panel-Community><ul><li><a href="/zh-cn/plugins/" class="menu-link">内置插件列表</a></li><li><a href="/zh-cn/contributing.html" class="menu-link">如何贡献</a></li><li><a href="/zh-cn/resource.html" class="menu-link">资源</a></li><li><a href="/zh-cn/faq.html" class="menu-link">常见问题</a></li></ul></dd></dl>
</aside>
<script>
var mobileTrigger = document.getElementById('mobileTrigger');
var mobileAside = document.getElementById('mobileAside');

var expandMenu = function(title) {
  // handle icon
  const collapseIcon = document.getElementById('collapse-icon-' + title);
  if (collapseIcon) {
    collapseIcon.className = 'icon opend';
  }
  // handle panelEle
  const panelEle = document.getElementById('panel-' + title);
  if (panelEle) {
    panelEle.className = '';
  }
}

var collapseMenu = function(title) {
  // handle icon
  const collapseIcon = document.getElementById('collapse-icon-' + title);
  if (collapseIcon) {
    collapseIcon.className = 'icon closed';
  }
  // handle panelEle
  const panelEle = document.getElementById('panel-' + title);
  if (panelEle) {
    panelEle.className = 'aside-panel-hidden';
  }
}

mobileAside.onclick = function(e) {
  const targetId = e.target.id;
  if (targetId && (targetId.indexOf('title-') > -1 || targetId.indexOf('collapse-icon-') > -1)) {
    const title = targetId.replace('title-', '').replace('collapse-icon-', '');
    try { 
      // the the browser may have no localStroage or JSON.parse may throw exception.
      const menuInfo = JSON.parse(window.localStorage.getItem('menuInfo'));
        
      // current menu status
      const curClosed = menuInfo[title] ? menuInfo[title].closed : false; // default false

      // change UI
      curClosed ? expandMenu(title) : collapseMenu(title);

      // save menuInfo to localStorage
      menuInfo[title] = { closed: !curClosed } // opposite
      window.localStorage.setItem('menuInfo', JSON.stringify(menuInfo));
    } catch (e) {}
  }
};

mobileTrigger.onclick = function(e) {
  e.preventDefault();
  if (mobileAside.className.indexOf('mobile-show') === -1) {
    mobileAside.className += ' mobile-show';
  } else {
    mobileAside.className = 'toc';
  }
};

(function() {
  // save data to localStorage because the page will refresh when user change the url.
  let menuInfo;
  try { 
    // the the browser may have no localStroage or JSON.parse may throw exception.
    menuInfo = JSON.parse(window.localStorage.getItem('menuInfo'));
    if (!menuInfo) {
      menuInfo = {};
      window.localStorage.setItem('menuInfo', JSON.stringify(menuInfo));
    }
  } catch (e) {
    menuInfo = {}; // default {}
  }

  for (const title in menuInfo) {
    if (menuInfo[title] && menuInfo[title].closed) { // menu in closed status.
      collapseMenu(title);
    } else {
      expandMenu(title);
    }
  }

  // highlight menu
  const pathname = window.location.pathname;
  const selector = `a[href="${pathname}"].menu-link,a[href="${pathname}index.html"].menu-link`;
  const menuItem = mobileAside.querySelector(selector);
  if (menuItem) { menuItem.className += ' highlight'; }
})();
</script>

</div>

  </div>
</body>
<script src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script>
<script>
docsearch({
  apiKey: '1561de31a86f79507ea00cdb54ce647c',
  indexName: 'eggjs',
  inputSelector: '#search-query',
});
</script>
<div class="cnzz">
<script src="https://s11.cnzz.com/z_stat.php?id=1261142226&web_id=1261142226" language="JavaScript"></script>
</div>

</html>
